import { Component, OnInit, ElementRef } from '@angular/core';
import { Router } from '@angular/router';
import { HeroService } from '../../../../services/hero.service';
import { NzMessageService,NzModalService } from 'ng-zorro-antd';

@Component({
  selector: 'app-editpassword',
  templateUrl: './editpassword.component.html',
  styleUrls: ['./editpassword.component.css']
})
export class EditpasswordComponent implements OnInit {
  
  btn_loading=false;
  public oldpassword:any=null;
  public newpassword:string;
  public newpassword2:string;
  public oldpwdcode:number;
  public newpwdcode:number;
  public newpwdcode2:number;

  constructor(private heroService: HeroService,
              private router: Router,
              private el: ElementRef,
              private message: NzMessageService,
              private modalService: NzModalService) { }

  ngOnInit() {
  }
// 修改密码
  editPwd(): void {
    if(!this.oldpassword){
      this.oldpwdcode=-1;
    }
    if(!this.newpassword){
      this.newpwdcode=-1;
    }
    if(!this.newpassword2){
      this.newpwdcode2=-1;
    }
    if(this.oldpwdcode==-1 || this.newpwdcode==-1 ||  this.newpwdcode2==-1){
      
    }else{
      if(this.newpassword !== this.newpassword2){
        this.newpwdcode2=-2;
      }else{
        this.btn_loading=true;
        let url=host+"/main/editPwd/save";
        let model='oldPwd='+this.oldpassword+'&password='+this.newpassword;
        this.heroService.post(url,model).then(result=>{
            this.btn_loading=false;
            if(result.code==0){
              this.message.success('修改密码成功！');
              this.router.navigate(['/main/index']);
            }else{
              this.modalService.error({
                nzTitle: '提示',
                nzContent: result.msg
              });
            }
        })
      }
    }
  }
  onFocusOld(){
    this.oldpwdcode=0;
    this.el.nativeElement.querySelector('#testInput').readOnly = false;
  }
  onFocusNew(){
    this.newpwdcode=0;
  }
  onFocusNewC(){
    this.newpwdcode2=0;
  }
}
